package com.ming.closeto.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.ming.closeto.R;
import com.ming.closeto.ui.CacheClearActivity;
import com.ming.closeto.ui.FileBrowseActivity;
import com.ming.closeto.ui.ScreenBadPointActivity;
import com.ming.closeto.ui.SystemInfoActivity;
import com.ming.closeto.utils.FileUtils;
import com.ming.closeto.utils.LogUtils;

import java.util.ArrayList;

/**
 * 系统
 */
public class SystemFragment extends Fragment implements View.OnClickListener {
    private LinearLayout ll_screen_op;
    private LinearLayout ll_file_br;
    private LinearLayout ll_system_info;
    private LinearLayout ll_cache_clear;

    private PieChart mPieChart;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_system, null);
        initView(view);
        return view;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    private void initView(View view) {
        ll_screen_op = (LinearLayout) view.findViewById(R.id.ll_screen_op);
        ll_file_br = (LinearLayout) view.findViewById(R.id.ll_file_br);
        ll_system_info = (LinearLayout) view.findViewById(R.id.ll_system_info);
        ll_cache_clear = (LinearLayout) view.findViewById(R.id.ll_cache_clear);

        mPieChart = view.findViewById(R.id.mPieChart);

        ll_screen_op.setOnClickListener(this);
        ll_file_br.setOnClickListener(this);
        ll_system_info.setOnClickListener(this);
        ll_cache_clear.setOnClickListener(this);

        initPieChart();
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    private void initPieChart() {
        mPieChart.setUsePercentValues(true);
        mPieChart.getDescription().setEnabled(false);
        mPieChart.setExtraOffsets(5, 10, 5, 5);

        mPieChart.setDragDecelerationFrictionCoef(0.95f);
        //设置中间文件
        //mPieChart.setCenterText(generateCenterSpannableText());

        mPieChart.setDrawHoleEnabled(false);
        mPieChart.setHoleColor(Color.WHITE);

        mPieChart.setTransparentCircleColor(Color.WHITE);
        mPieChart.setTransparentCircleAlpha(110);

        mPieChart.setHoleRadius(58f);
        mPieChart.setTransparentCircleRadius(61f);

        mPieChart.setDrawCenterText(true);

        mPieChart.setRotationAngle(0);
        // 触摸旋转
        mPieChart.setRotationEnabled(true);
        mPieChart.setHighlightPerTapEnabled(true);

        //变化监听
        //mPieChart.setOnChartValueSelectedListener(this);

        //模拟数据
        ArrayList<PieEntry> entries = new ArrayList<PieEntry>();

        //总内存 / 已用内存
        //总大小
        long allSize = FileUtils.getSDSize(0);
        //已用大小
        long fixSize = FileUtils.getSDSize(1);
        //2080194560  1758728192

        int  fixSizes = (int)((float)((float)fixSize / (float) allSize) * 100);
        LogUtils.i("fixSizes:" + fixSizes);
        //剩余带下
        //FileUtils.getSDSize(0) - FileUtils.getSDSize(1);

        entries.add(new PieEntry(fixSizes, "已用大小"));
        entries.add(new PieEntry(100 - fixSizes, "剩余大小"));

        //设置数据
        setData(entries);

        mPieChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);

        Legend l = mPieChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        l.setOrientation(Legend.LegendOrientation.VERTICAL);
        l.setDrawInside(false);
        l.setXEntrySpace(7f);
        l.setYEntrySpace(0f);
        l.setYOffset(0f);
        l.setTextColor(Color.WHITE);

        // 输入标签样式
        mPieChart.setEntryLabelColor(Color.BLACK);
        mPieChart.setEntryLabelTextSize(12f);
    }

    private void setData(ArrayList<PieEntry> entries) {
        PieDataSet dataSet = new PieDataSet(entries, "系统内存");
        dataSet.setSliceSpace(3f);
        dataSet.setSelectionShift(5f);

        //数据和颜色
        ArrayList<Integer> colors = new ArrayList<Integer>();
        for (int c : ColorTemplate.VORDIPLOM_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.JOYFUL_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.COLORFUL_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.LIBERTY_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.PASTEL_COLORS)
            colors.add(c);
        colors.add(ColorTemplate.getHoloBlue());
        dataSet.setColors(colors);
        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(11f);
        data.setValueTextColor(Color.WHITE);
        mPieChart.setData(data);
        mPieChart.highlightValues(null);
        //刷新
        mPieChart.invalidate();
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();

        switch (v.getId()) {
            case R.id.ll_screen_op:
                intent.setClass(getActivity(), ScreenBadPointActivity.class);
                break;
            case R.id.ll_file_br:
                intent.setClass(getActivity(), FileBrowseActivity.class);
                break;
            case R.id.ll_system_info:
                intent.setClass(getActivity(), SystemInfoActivity.class);
                break;
            case R.id.ll_cache_clear:
                intent.setClass(getActivity(), CacheClearActivity.class);
                break;
        }

        startActivity(intent);
    }
}
